<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="visible" width="600px" v-if="visible" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :disabled="disabled">
        <el-form-item label="调整数量" prop="num">
          <el-input v-model="form.num" placeholder="请填写调整数量" :style="{width: inputWidth}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(1)" :loading="addLoading">添加库存</el-button>
        <el-button type="primary" @click="submitForm(2)" :loading="releaseLoading">释放库存</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    inventoryIssueUpdateBatch,
    inventoryBoxUpdateBatch,
    inventoryChangeUpdateBatch,
    inventoryCastingUpdateBatch,
    inventoryTreasureBatch
  } from "@/api/equityManager/equityCard";

  export default {
    // dicts: ['asset_card_type'],
    name: "Dialog",
    data() {
      //调整数量校验
      var checkNum = (rule, value, callback) => {
        var reg = /^[1-9]\d*$/;
        if (!value) {
          return callback(new Error('请填写调整数量'));
        }
        // else if (value < 1 || value > 99999) {
        //   return callback(new Error('请输入1-99999'));
        // } 
        else if (!reg.test(value)) {
          return callback(new Error('请输入正确数值'));
        } else {
          callback();
        }
      };
      return {
        labelWidth: '120px',
        inputWidth: '300px',
        id: '', //
        addLoading: false,
        releaseLoading: false,
        // 是否禁用表单
        disabled: false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        visible: false,
        status: 1, //1:发行库存调整   2:盲盒库存调整
        // 表单参数
        form: {
          num: '', //调整数量
          idList: [], //数字权益卡id數組
          type: '', //调整类型 1 添加库存 2 释放库存
        },
        // 表单校验
        rules: {
          num: [{
            required: true,
            // message: "请填写调整数量",
            validator: checkNum,
            trigger: ["blur", "change"]
          }],
        },
      };
    },
    created() {

    },
    methods: {
      /** 打开发行管理弹框 */
      open(status,productIds) {
        this.reset();
        this.status = status
        this.form.idList = productIds
        if (status == 1) {
          this.title = "发行库存调整"
        } else if (status == 2) {
          this.title = "盲盒库存调整"
        } else if (status == 3) {
          this.title = "兑换库存调整"
        }else if (status == 4)  {
          this.title = "合成库存调整"
        }else if (status == 5)  {
          this.title = "数字宝箱库存调整"
        }
        this.visible = true
      },
      // 取消按钮
      cancel() {
        this.visible = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form.num = ''
      },
      /** 提交按钮 */
      submitForm(type) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.type = type
            if (type == 1) {
              this.addLoading = true
            } else {
              this.releaseLoading = true
            }
            if (this.status == 1) { //发行库存调整
              inventoryIssueUpdateBatch(this.form).then(response => {
                this.$message.success('操作成功');
                this.cancel();
                this.$emit('refresh')
                this.addLoading = false
                this.releaseLoading = false
              }).catch(() => {
                this.addLoading = false
                this.releaseLoading = false
              });
            } else if (this.status == 2) { //盲盒库存调整
              inventoryBoxUpdateBatch(this.form).then(response => {
                this.$message.success('操作成功');
                this.cancel();
                this.$emit('refresh')
                this.addLoading = false
                this.releaseLoading = false
              }).catch(() => {
                this.addLoading = false
                this.releaseLoading = false
              });
            } else if(this.status == 3) { //兑换库存调整
              inventoryChangeUpdateBatch(this.form).then(response => {
                this.$message.success('操作成功');
                this.cancel();
                this.$emit('refresh')
                this.addLoading = false
                this.releaseLoading = false
              }).catch(() => {
                this.addLoading = false
                this.releaseLoading = false
              });
            }else if(this.status == 4){ //合成库存调整 
              inventoryCastingUpdateBatch(this.form).then(response => {
                this.$message.success('操作成功');
                this.cancel();
                this.$emit('refresh')
                this.addLoading = false
                this.releaseLoading = false
              }).catch(() => {
                this.addLoading = false
                this.releaseLoading = false
              });
            }else if(this.status == 5){ //数字宝箱库存调整 
              inventoryTreasureBatch(this.form).then(response => {
                this.$message.success('操作成功');
                this.cancel();
                this.$emit('refresh')
                this.addLoading = false
                this.releaseLoading = false
              }).catch(() => {
                this.addLoading = false
                this.releaseLoading = false
              });
            }
          }
        });
      },
    }
  };

</script>
<style scoped>
  .img_box {
    width: 100px;
    height: 70px;
    border-radius: 4px;
  }

</style>
